package com.ymm.spark.test;

import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author zhangj
 * @date 2019/10/25
 */
@Slf4j
public class JdbcCRUD {
	public static void main(String[] args) {
		Connection conn = null;
		Statement stmt = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/spark_project?useUnicode=true&characterEncoding=utf8",
					"root",
					"qazwsX1234");
			stmt = conn.createStatement();
			String sql = "insert into test_user(name,age) values('李四',26)";
			int rtn = stmt.executeUpdate(sql);

			System.out.println("SQL语句影响了【" + rtn + "】行。");
		} catch (Exception e) {
			log.error("jdbc连接出错:{}", e.getMessage(), e);
		} finally {
			try {
				if (stmt != null) {
					stmt.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
		}
	}
}
